package com.edu.internship.busi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.internship.busi.dto.PermissionDTO;
import com.edu.internship.busi.dto.RoleDTO;
import com.edu.internship.busi.entity.Permission;
import com.edu.internship.busi.entity.Role;
import com.edu.internship.busi.entity.RolePermission;
import com.edu.internship.busi.service.IPermissionService;
import com.edu.internship.busi.service.IRolePermissionService;
import com.edu.internship.busi.service.IRoleService;
import com.edu.internship.util.Tools;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("api/permission")
@Slf4j
@Api(tags = {"权限的控制器"})
public class PermissionController {
    @Resource
    private IPermissionService permissionService;
    @Resource
    private IRolePermissionService rolePermissionService;

    @GetMapping
    @ApiOperation(value = "分页查询-Permission", notes = "根据条件进行查询-Permission")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", dataTypeClass = Long.class, paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页几条记录", dataTypeClass = Long.class, paramType = "query", defaultValue = "10")
    })
    public IPage<Permission> queryForRole(PermissionDTO dto, @RequestParam(defaultValue = "1") long current,
                                    @RequestParam(defaultValue = "10") long size){
        LambdaQueryWrapper<Permission> reQw = Wrappers.lambdaQuery();
        reQw.like(StringUtils.isNotBlank(dto.getName()),Permission::getName,dto.getName());
        List<Permission> permissions = permissionService.list(reQw);
        return permissionService.page(new Page<>(current,size),reQw);
    }


    @PostMapping("/created")
    @ApiOperation(value = "新增-Permission", notes = "新增-Permission")
    public boolean created(@RequestBody @Validated PermissionDTO dto){
        dto.setId(Tools.createRandomCode(5,true));
        return permissionService.created(dto);
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除-Permission",notes = "删除-Permission")
    public boolean remove(@RequestBody PermissionDTO dto){
        LambdaQueryWrapper<RolePermission> rpQw = Wrappers.lambdaQuery();
        rpQw.eq(StringUtils.isNotBlank(dto.getId()),RolePermission::getPId,dto.getId());
        rolePermissionService.remove(rpQw);
        return permissionService.remove(dto.getId());
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新-Permission",notes = "更新-Permission")
    public boolean update(@RequestBody @Validated PermissionDTO dto){
        return permissionService.update(dto);
    }

    @GetMapping("queryById")
    @ApiOperation(value = "根据id查询-Permission",notes ="根据id查询-Permission")
    public Permission queryById(String id){
        return permissionService.getById(id);

    }

    @PostMapping("/change")
    @ApiOperation(value = "禁用启用",notes = "禁用启用")
    public boolean change(@RequestBody List<String> id){
        List<Permission> permissions = permissionService.change(id);
        if (permissions.get(0).getStatus().equals("1")) {
            permissions.get(0).setStatus("2");
        } else {
            permissions.get(0).setStatus("1");
        }
        permissionService.updateById(permissions.get(0));

        return true;
    }

    @GetMapping("/get")
    @ApiOperation(value = "查询Permission",notes = "查询Permission")
    public List<String> getPermission(){
        LambdaQueryWrapper<Permission> pQw = Wrappers.lambdaQuery();
        pQw.eq(Permission::getStatus,1);
        return permissionService.list(pQw).stream().map(Permission::getComponent).collect(Collectors.toList());
    }



}
